#include<bits/stdc++.h>
#define int long long
using namespace std;
namespace IO
{
	char buff[1<<21],*p1=buff,*p2=buff;
	char getch(){return p1==p2&&(p2=((p1=buff)+fread(buff,1,1<<21,stdin)),p1==p2)?EOF:*p1++;}
	template<typename T>void read(T &x){char CH=getch();int fl=1;x=0;while(CH>'9'||CH<'0'){if(CH=='-')fl=-1;CH=getch();}while(CH<='9'&&CH>='0'){x=x*10+CH-48;CH=getch();}x*=fl;}
	template<typename T>void read_s(T &x){x="";char CH=getch();while(!(CH>='a'&&CH<='z')&&!(CH>='A'&&CH<='Z'))CH=getch();while((CH>='a'&&CH<='z')||(CH>='A'&&CH<='Z')){x+=CH;CH=getch();}}
	template<typename T,typename ...Args>void read(T &x,Args& ...args){read(x);read(args...);}
	char obuf[1<<21],*p3=obuf;
	void putch(char CH) {if(p3-obuf<(1<<21))*p3++=CH;else fwrite(obuf,p3-obuf,1,stdout),p3=obuf,*p3++=CH;}
	char CH[100];
	template<typename T>void write(T x) {if(!x)return putch('0');if(x<0)putch('-'),x*=-1;int top=0;while(x)CH[++top]=x%10+48,x/=10;while(top)putch(CH[top]),top--;}
	template<typename T,typename ...Args>void write(T x,Args ...args) {write(x);putch(' ');write(args...);}
	void put(string s){for(int i=0;i<s.size();i++)putch(s[i]);}
	void flush(){fwrite(obuf,p3-obuf,1,stdout);}
}
using namespace IO;
int n;
struct point
{
	int x,y;
}a[500005];
namespace Shiroha
{
	int t[1000005],cnt[1000005];
	inline int lowbit(int x){return x&-x;}
	inline void add(int x,int v){x+=5e5;for(;x<=1e6;x+=lowbit(x))t[x]+=v,++cnt[x];}
	inline int query(int x){x+=5e5;int res=0;for(;x;x-=lowbit(x))res+=t[x];return res;}
	inline int qcnt(int x){x+=5e5;int res=0;for(;x;x-=lowbit(x))res+=cnt[x];return res;}
}using namespace Shiroha;
map<int,int>id[500005];
struct node
{
	int nxt,to;
}e[2000005];
int head[500005],cnt_edge;
void add_edge(int u,int v)
{
	e[++cnt_edge].to=v;
	e[cnt_edge].nxt=head[u];
	head[u]=cnt_edge;
}
const int dx[]={0,0,1,-1};
const int dy[]={1,-1,0,0};
int dis[500005];
inline void bfs(int s)
{
	memset(dis,-1,sizeof dis);dis[s]=0;
	queue<int>q;q.push(s);
	while(q.size())
	{
		int u=q.front();q.pop();
		for(int i=head[u];i;i=e[i].nxt)
		{
			int v=e[i].to;
			if(dis[v]==-1) dis[v]=dis[u]+1,q.push(v);
		}
	}
}
int f[500005];
int find(int x){return f[x]==x?x:f[x]=find(f[x]);}
int sum,siz[500005];
void dfs(int u,int fa)
{
	siz[u]=1;
	for(int i=head[u];i;i=e[i].nxt)
	{
		int v=e[i].to;
		if(v==fa) continue;
		dfs(v,u);siz[u]+=siz[v];
		sum+=siz[v]*(n-siz[v]);
	}
}
signed main()
{
	freopen("game.in","r",stdin);
	freopen("game.out","w",stdout);
	read(n);
	for(int i=1;i<=n;i++) read(a[i].x,a[i].y),id[a[i].x][a[i].y]=i;
	for(int i=1;i<=n;i++)
		for(int j=0;j<4;j++)
		{
			int nx=a[i].x+dx[j],ny=a[i].y+dy[j];
			if(id[nx].find(ny)!=id[nx].end()) add_edge(i,id[nx][ny]);
		}
	if(n<=0)
	{
		int ans=0;
		for(int i=1;i<=n;i++) 
		{
			bfs(i);
			for(int j=i;j<=n;j++) ans+=dis[j];
		}
		write(ans);
	}
	else
	{
		for(int i=1;i<=n;i++) f[i]=i;int flag=1;
		for(int i=1;i<=n;i++) 
			for(int j=head[i];j;j=e[j].nxt)
			{
				int v=e[j].to;
				if(v>i) continue;
				if(find(i)==find(v)) flag=0;
				else f[find(i)]=find(v);
			}
		if(!flag)
		{
			int ans=0;
			memset(t,0,sizeof t);
			memset(cnt,0,sizeof cnt);
			sort(a+1,a+n+1,[](point x,point y){return x.x==y.x?x.y<y.y:x.x<y.x;});
			for(int i=1;i<=n;i++) ans+=query(a[i].y)+(a[i].x+a[i].y)*qcnt(a[i].y),add(a[i].y,-a[i].y-a[i].x);
			memset(t,0,sizeof t);
			memset(cnt,0,sizeof cnt);
			sort(a+1,a+n+1,[](point x,point y){return x.x==y.x?x.y>y.y:x.x<y.x;});
			for(int i=1;i<=n;i++) ans+=query(1000000)-query(a[i].y-1)+(a[i].x-a[i].y)*(qcnt(1000000)-qcnt(a[i].y-1))
			,add(a[i].y,a[i].y-a[i].x);
			
			for(int i=1;i<=n;i++) a[i].x=-a[i].x;
			memset(t,0,sizeof t);
			memset(cnt,0,sizeof cnt);
			int res=0;
			sort(a+1,a+n+1,[](point x,point y){return x.x==y.x?x.y<y.y:x.x>y.x;});
			int cur=1;
			for(int i=1;i<=n;i++) 
			{
				while(cur<i&&a[cur].x>a[i].x) add(a[cur].y,-a[cur].y+a[cur].x),++cur;
				ans+=query(a[i].y)+(-a[i].x+a[i].y)*qcnt(a[i].y);
			}
			memset(t,0,sizeof t);
			memset(cnt,0,sizeof cnt);
			sort(a+1,a+n+1,[](point x,point y){return x.x==y.x?x.y>y.y:x.x>y.x;});
			cur=1;
			for(int i=1;i<=n;i++) 
			{
				while(cur<i&&a[cur].x>a[i].x) add(a[cur].y,-a[cur].y-a[cur].x),++cur;
				ans+=query(1000000)-query(a[i].y-1)+(-a[i].x-a[i].y)*(qcnt(1000000)-qcnt(a[i].y-1));
			}
		}
		else
		{
			dfs(1,0);
			write(sum);
		}
	}
	flush();
	return 0;
}

